package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.model.Appendix;
import com.enterprise.projectManager.model.Count;
import com.enterprise.projectManager.rope.*;
import com.enterprise.projectManager.service.CountService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CountServiceImpl implements CountService {

    private final CountRope countRope;
    private final AppendixRope appendixRope;
    private final ContractRope contractRope;
    private final LogRope logRope;
    private final PayProgressRope payProgressRope;
    private final ProblemRope problemRope;
    private final ProjectRope projectRope;
    private final QualityCheckRope qualityCheckRope;
    private final UserRope userRope;
    @Override
    public String generalAppendixId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setAppendixCount(count.getAppendixCount()+1);
        if (countRope.save(count) != null) return "FJ"+count.getAppendixCount();
        return null;
    }

    @Override
    public String generalContractId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setContractCount(count.getContractCount()+1);
        if (countRope.save(count) != null) return "HT"+count.getContractCount();
        return null;
    }

    @Override
    public String generalLogId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setLogCount(count.getLogCount()+1);
        if (countRope.save(count) != null) return "RZ"+count.getLogCount();
        return null;
    }

    @Override
    public String generalPayProgressId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setPayProgressCount(count.getPayProgressCount()+1);
        if (countRope.save(count) != null) return "ZFJD"+count.getPayProgressCount();
        return null;
    }

    @Override
    public String generalProblemId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setProblemCount(count.getProblemCount()+1);
        if (countRope.save(count) != null) return "WT"+count.getProblemCount();
        return null;
    }

    @Override
    public String generalProjectId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setProjectCount(count.getProjectCount()+1);
        if (countRope.save(count) != null) return "XM"+count.getProjectCount();
        return null;
    }

    @Override
    public String generalQualityCheckId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setQualityCheckCount(count.getQualityCheckCount()+1);
        if (countRope.save(count) != null) return "ZJ"+count.getQualityCheckCount();
        return null;
    }

    @Override
    public String generalUserId() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setUserCount(count.getUserCount()+1);
        if (countRope.save(count) != null) return "YH"+count.getUserCount();
        return null;
    }

    @Override
    public Count synchronization() {
        Count count = countRope.findAll(PageRequest.of(0, 1)).getContent().get(0);
        count.setUserCount(userRope.findAll().size()+100);
        count.setAppendixCount(appendixRope.findAll().size()+100);
        count.setLogCount(logRope.findAll().size()+100);
        count.setContractCount(contractRope.findAll().size()+100);
        count.setProblemCount(problemRope.findAll().size()+100);
        count.setPayProgressCount(payProgressRope.findAll().size()+100);
        count.setProjectCount(projectRope.findAll().size()+100);
        count.setQualityCheckCount(qualityCheckRope.findAll().size()+100);
        return countRope.save(count);
    }
}
